<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$this->model('member')->upmember();
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	if(!empty($_SESSION['openid'])){
		$openid = $_SESSION['openid'];
		$fansfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'openid'=>$_SESSION['openid']),'follow');
	}
	$return['member']= $this->model('member')->getmember();
	$return['member']['title']=cfc('tools')->substr($return['member']['title'],0,'10');
	$return['openid']= $_SESSION['openid'];
	$return['llm_models2']=$this->llm_models2;
	$return['sex2']=array(
		array('id'=>1,'title'=>'男'),
		array('id'=>2,'title'=>'女'),
	);
	$return['member']['sex2']=$return['member']['sex']==1?0:1;
	//获取当前时间：
	$showtypetime1=mktime(10,30,0,date('m'),date('d'),date('Y'));
	$showtypetime2=mktime(14,00,0,date('m'),date('d'),date('Y'));
	$showtypetime3=mktime(17,00,0,date('m'),date('d'),date('Y'));
	$showtypetime4=mktime(21,00,0,date('m'),date('d'),date('Y'));
	if(TIMESTAMP<$showtypetime1){
		$return['showtype']=1;
	}elseif(TIMESTAMP>$showtypetime1 && TIMESTAMP<$showtypetime2){
		$return['showtype']=2;
	}elseif(TIMESTAMP>$showtypetime2 && TIMESTAMP<$showtypetime3){
		$return['showtype']=3;
	}elseif(TIMESTAMP>$showtypetime3 && TIMESTAMP<$showtypetime4){
		$return['showtype']=4;
	}elseif(TIMESTAMP>$showtypetime4){
		$return['showtype']=5;
	}
	$return['showtypetitle']=$this->showdaytype[$return['showtype']];
	$return['params']=array('groupid'=>0);
	$return['post']=array('msg'=>'','issikao'=>1,'iswww'=>1);
	$return['sharedata']=array(
		'title'=>$return['basic']['share_title'],
		'desc'=>$return['basic']['share_desc'],
		'link'=>custom_url('jy_agentmcp/index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['basic']['share_image'],
	);
	set_html('title',$return['pluginset']['title']?:'AI智能体MCP');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=$_RQ['keyword'];
	}
	if($_RQ['llm_model']){
		$params['llm_model like']='%'.trim($_RQ['llm_model']).'%';
		$return['params']['llm_model']=$_RQ['llm_model'];
	}
	$fields='';
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_agentmcp_usergoods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['logo']=tomedia($item['logo']);
			$item['desc']=cfc('tools')->substr($item['desc'],0,'38');
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	exi($return);
}
if($_RQ['op']=='checkbody'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请先完善昵称！','error');
	}
	if(!$postdata['sex']){
		exi('请先完善性别！','error');
	}
	if($_SESSION['wxapp_id'] && $_SESSION['container']=='wxapp'){
		$result=model('core/wxapp_api')->msgSecCheck($postdata['title']);
		if($result['errcode']){
			exi('昵称可能不合法！','error');
		}
	}
	pdo_update('jy_agentmcp_member',array('sex'=>$postdata['sex'],'title'=>$postdata['title']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	exi('更新成功！');
}
if($_RQ['op']=='peigoodscode'){
	$postdata=get_postdata();
	$postdata['code']=trim($postdata['code']);
	if(!$postdata['code']){
		exi('请输入设备播报的6位数验证码','error');
	}
	if (!preg_match('/^\d{6}$/', $postdata['code'])) {
		exi('验证码格式不正确，请输入6位数字','error');
	}
	if(!$postdata['agentid'] && !$postdata['goodslogid']){
		exi('请选择智能体/设备！','error');
	}
	if($postdata['goodslogid']){
		$postdata['agentid']=pdo_getcolumn('jy_agentmcp_goodslog',array('id'=>$postdata['goodslogid'],'uniacid'=>$_SESSION['uniacid']),'agentid');
	}
	if(!$postdata['agentid']){
		exi('该设备未绑定智能体！','error');
	}
	$postdata['agentapiid']=pdo_getcolumn('jy_agentmcp_usergoods',array('id'=>$postdata['agentid'],'uniacid'=>$_SESSION['uniacid']),'apiid');
	if(!$postdata['agentapiid']){
		$agent=pdo_get('jy_agentmcp_usergoods',array('id'=>$postdata['agentid'],'uniacid'=>$_SESSION['uniacid']));
		$voice=pdo_get('jy_agentmcp_voice',array('id'=>$agent['voiceid'],'uniacid'=>$_SESSION['uniacid']));
		$agent['tts_voice']=$voice['voiceid'];
		$agent['language']=$voice['language'];
		$res=$this->model('api')->addgoodsagent('',$agent);
		pdo_update('jy_agentmcp_usergoods',array('apiid'=>trim($res['id'])),array('id'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
		$postdata['agentapiid']=$res['id'];
	}
	$this->model('agent')->peigoodscode($postdata);
	exi('添加成功！','','/index/');
}
if($_RQ['op']=='delgoods'){
	$postdata=get_postdata();
	$postdata['id']=trim($postdata['id']);
	if(!$postdata['id']){
		exi('请选择设备！','error');
	}
	$goodslog=pdo_get('jy_agentmcp_usergoods',array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
	if($goodslog['agentapiid'] && $goodslog['deviceid']){
		//$this->model('api')->delgoods($goodslog,1);
	}
	pdo_delete('jy_agentmcp_usergoods',array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
	exi('删除成功！','','/index/');
}